package com.kaishengit.service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import com.kaishengit.dao.IdeaDao;
import com.kaishengit.dao.MessageDao;
import com.kaishengit.dao.UserDao;
import com.kaishengit.entity.Idea;
import com.kaishengit.entity.Message;
import com.kaishengit.entity.User;
import com.kaishengit.entity.UserIdea;
import com.kaishengit.util.DateUtil;
import com.kaishengit.util.PKutil;
import com.kaishengit.util.Page;
import com.kaishengit.util.SubS;

public class IdeaService {
	private IdeaDao dao=new IdeaDao();
	private UserDao udao=new UserDao();
	private Idea idea=new Idea();
	private MessageDao mdao=new MessageDao();
	
	public Page<UserIdea> findAllByProjectId(int pageNum, String projectid, String nowtime){
		Page<UserIdea> page = new Page<UserIdea>();
		page.setPageNum(pageNum);
		page.setPageSize(5);
		page.setTotalCount(dao.findAount(projectid));
		page.getTotalCount();
		List<Idea> idealist = dao.findAllByProjectId(page.getOffest(),page.getPageSize(),projectid);
		List<UserIdea> userlist=new ArrayList<UserIdea>();
		for(Idea idea:idealist){
			UserIdea ui=new UserIdea();
			String ideatime=idea.getCreatetime();
			try {
				String result=DateUtil.test(nowtime, ideatime);
				ui.setResult(result);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			String time;
			try {
				time=DateUtil.getAccount(DateUtil.getNow(), idea.getCreatetime());
				idea.setCreatetime(time);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			
			ui.setIdea(idea);
			User user=udao.findById(idea.getT_user_id());
			ui.setUser(user);
			userlist.add(ui);
		}
		page.setList(userlist);
		return page;
	}


	public void save(String title, String content, String userid, String projectid) {
		String time=DateUtil.getNow();
		String ideaid=PKutil.getPk();
		idea.setId(ideaid);
		idea.setTitle(title);
		idea.setContent(content);
		idea.setT_project_id(projectid);
		idea.setT_user_id(userid);
		idea.setCreatetime(time);
		dao.save(idea);
		
		/**
		 * 将想法的信息保存进message表中
		 * */
		Message m=new Message();
		String meid=PKutil.getPk();
		String con=SubS.getSubString(content);
		m.setContent(con);
		m.setDate(time);
		m.setIdeaid(ideaid);
		m.setType("idea");
		m.setId(meid);
		m.setName(title);
		m.setUserid(userid);
		m.setProjectid(projectid);
		mdao.saveIdea(m);
		
	}


	public UserIdea findByIdeaId(String ideaid){
		Idea idea=dao.findByIdeaId(ideaid);
		UserIdea ui=new UserIdea();
		String time;
		try {
			time=DateUtil.getAccount(DateUtil.getNow(), idea.getCreatetime());
			idea.setCreatetime(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		ui.setIdea(idea);
		ui.setUser(udao.findById(idea.getT_user_id()));
		return ui;
		
	}
	
	public Idea findByIdea(String ideaid){
		return dao.findByIdeaId(ideaid);
		
	}


	public void update(String ideaid,String title, String createtime, String desc,
			String projectid, String userid) {
		dao.update(ideaid,title,createtime,desc,projectid,userid);
	}


	public void delete(String ideaid) {
		dao.del(ideaid);
	}


	
	
	
}
